/**
* @author:kellen
* @contact:xiekellen@hotmail.com
* @company:achivo-gz
* @created on:
* @modified on:
*/

package com.sasis.dao.ibatis;

import java.sql.SQLException;
import java.util.List;

import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.ibatis.SqlMapClientCallback;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.sasis.dao.MsgReleaseDao;
import com.sasis.model.MsgRelease;

public class MsgReleaseDaoiBatis extends BaseDaoiBATIS implements MsgReleaseDao {

    /**
     * @see com.sasis.dao.MsgReleaseDao#getMsgReleases(com.sasis.model.MsgRelease)
     */
    public List getMsgReleases(final MsgRelease msgRelease) {
          return getSqlMapClientTemplate().queryForList("getMsgReleases", msgRelease);
    }
    public List getPopMsgReleases(final MsgRelease msgRelease) {
        return getSqlMapClientTemplate().queryForList("getPopMsgReleases", msgRelease);
    }
    
    /**
     * @see com.sasis.dao.MsgReleaseDao#getMsgRelease(Integer id)
     */
    public MsgRelease getMsgRelease(Integer id) {
        MsgRelease msgRelease = (MsgRelease) getSqlMapClientTemplate().queryForObject("getMsgRelease", id);

        //if (msgRelease == null) {
            //throw new ObjectRetrievalFailureException(MsgRelease.class, id);
        //}

        return msgRelease;
    }
    
    public String getMsgReleaseUser(Integer id) {
    	StringBuffer sb = new StringBuffer();
        List list = getSqlMapClientTemplate().queryForList("getMsgReleaseUser", id);
        if(null==list) return null;
       for(int i=0;i<list.size();i++){
    	   MsgRelease msgRelease = (MsgRelease)list.get(i);
    	   
    	   sb.append(msgRelease.getCnName()).append(",");
       }
       
       return sb.toString();
    }

    /**
     * @see com.sasis.dao.MsgReleaseDao#saveMsgRelease(MsgRelease msgRelease)
     */    
    public int saveMsgRelease(final MsgRelease msgRelease) {
        Integer id = msgRelease.getId();
        String[] ids = msgRelease.getUserName().split(",");
        msgRelease.setIds(ids);
        // check for new record
        if (id == null) {
            id = (Integer) getSqlMapClientTemplate().insert("addMsgRelease", msgRelease);
            msgRelease.setId(id);
            
            
            
            getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
                public Object doInSqlMapClient(SqlMapExecutor executor)
                        throws SQLException {
                    executor.startBatch();
                    // do some iBatis operations here
                    
                    for(int i=0;i<msgRelease.getIds().length;i++)
                    {    
                    	
                    	msgRelease.setUserName(msgRelease.getIds()[i]);
                    	
                        executor.insert("addMsgUser", msgRelease);
                         
                    }
                    executor.executeBatch();
                    return null;
                }
            });
            
             
        } else {
            getSqlMapClientTemplate().update("updateMsgRelease", msgRelease);
            getSqlMapClientTemplate().insert("updateMsgUser", msgRelease);
        }
        if( id == null ) {
            throw new ObjectRetrievalFailureException(MsgRelease.class, id);
        }
        return id.intValue();
    }

    /**
     * @see com.sasis.dao.MsgReleaseDao#removeMsgRelease(Integer id)
     */
    public void removeMsgRelease(MsgRelease msgRelease) {
        getSqlMapClientTemplate().update("deleteMsgRelease",  msgRelease);
    }
}
